package com.hjxr.quota.datasource.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjxr.quota.datasource.domain.ColnameAlias;
import com.hjxr.quota.datasource.mapper.ColnameAliasMapper;
import com.hjxr.quota.datasource.service.ColnameAliasService;
import com.hjxr.quota.define.domain.QuotaSource;
import com.hjxr.quota.define.mapper.QuotaMapper;
import com.hjxr.quota.define.mapper.QuotaSourceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("colnameAliasService")
@Transactional(readOnly = true)
public class ColnameAliasServiceImpl extends ServiceImpl<ColnameAliasMapper, ColnameAlias> implements ColnameAliasService {

    @Resource
    private ColnameAliasMapper colnameAliasMapper;
    @Resource
    private QuotaSourceMapper quotaSourceMapper;
    @Resource
    private QuotaMapper quotaMapper;

    @Override
    public boolean update(ColnameAlias colnameAlias) {
        if (colnameAliasMapper.update(colnameAlias) > 0){
            if ("0".equals(colnameAlias.getIsquota())){
                Map<String, Object> m = new HashMap<>();
                m.put("COLNAME_ID", colnameAlias.getId());
                List<QuotaSource> qs_list = quotaSourceMapper.selectByMap(m);
                for (QuotaSource qs: qs_list){
                    quotaSourceMapper.deleteById(qs.getId());
                    quotaMapper.deleteById(qs.getQuotaId());
                }
            }
            return true;
        }
        return false;

    }

    @Override
    public List<ColnameAlias> selectColnameAlias(ColnameAlias colnameAlias) {
        return colnameAliasMapper.selectColnameAlias(colnameAlias);
    }

    @Override
    public List<ColnameAlias> selectByTablenameAliasId(Integer tablenameAliasId) {
        return colnameAliasMapper.selectByTablenameAliasId(tablenameAliasId);
    }

    @Override
    public ColnameAlias selectByColumnName(Integer tablenameAliasId, String columnName){
         return colnameAliasMapper.selectByColumnName(tablenameAliasId, columnName);
    }

}
